<template>
  <div class="kpi-container">
    <div ref="kpiChart" class="kpi-chart"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  data() {
    return {
      kpiChart: null,
    };
  },
  mounted() {
    this.kpiChart = echarts.init(this.$refs.kpiChart);
    this.GetList();
  },
  methods: {
    GetList() {
      const data = {
    data: [
        "53",
        "38",
        "24",
        "56",
        "29",
        "56"
    ]
}
          this.updateKPIChart(data)
//       this.$axios.post('/api/Analyse/kpiIndicatorDisplay')
//         .then(res => {
//           console.log(res.data);
//           const data = {
//     data: [
//         "53",
//         "38",
//         "24",
//         "56",
//         "29",
//         "56"
//     ]
// }
//           this.updateKPIChart(data)
//         })
//         .catch(error => {
//           console.error('Failed to fetch chart data:', error);
//         });
    },
    updateKPIChart(data) {
      const offsetX = 10;
      const offsetY = 5;

      // 自定义形状
      const CubeLeft = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          const xAxisPoint = shape.xAxisPoint;
          const c0 = [shape.x, shape.y];
          const c1 = [shape.x - offsetX, shape.y - offsetY];
          const c2 = [xAxisPoint[0] - offsetX, xAxisPoint[1] - offsetY];
          const c3 = [xAxisPoint[0], xAxisPoint[1]];
          ctx.moveTo(c0[0], c0[1]).lineTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).closePath();
        },
      });
      const CubeRight = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          const xAxisPoint = shape.xAxisPoint;
          const c1 = [shape.x, shape.y];
          const c2 = [xAxisPoint[0], xAxisPoint[1]];
          const c3 = [xAxisPoint[0] + offsetX, xAxisPoint[1] - offsetY];
          const c4 = [shape.x + offsetX, shape.y - offsetY];
          ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath();
        },
      });
      const CubeTop = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          const c1 = [shape.x, shape.y];
          const c2 = [shape.x + offsetX, shape.y - offsetY];
          const c3 = [shape.x, shape.y - offsetX];
          const c4 = [shape.x - offsetX, shape.y - offsetY];
          ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath();
        },
      });

      // 注册图形
      echarts.graphic.registerShape('CubeLeft', CubeLeft);
      echarts.graphic.registerShape('CubeRight', CubeRight);
      echarts.graphic.registerShape('CubeTop', CubeTop);

      // 修改后的图表选项
      const option = {
        title: {
          text: 'KPI 指标展示',
          show: false,
          textStyle: {
            color: '#fff',
            fontSize: 20,
          },
        },
        color: ['#097dc0', '#26D6D7'],
        grid: {
          top: '10%',
          left: '3%',
          right: 0,
          bottom: 0,
          containLabel: true,
        },
        tooltip: {},
        xAxis: {
          type: 'category',
          data: ['抓获', '现场抓获', '传唤到案', '电话通知', '主动投案', '自动投案'],
          axisLabel: {
            color: '#fff',
            fontSize: 12,
            interval: 0,
            formatter: function (value) {
              const maxLength = 2;
              if (value.length > maxLength) {
                return value.substring(0, maxLength) + '\n' + value.substring(maxLength);
              }
              return value;
            },
          },
          axisLine: {
            lineStyle: {
              color: 'rgb(41,188,245)',
            },
          },
          splitLine: {
            show: false,
            lineStyle: {
              color: '#233653',
            },
          },
          axisTick: {
            show: false,
          },
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            color: '#fff',
            // fontSize: 22,
          },
          axisLine: {
            lineStyle: {
              color: '#008de7',
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: 'rgba(255,255,255,0.2)'
            }
          },
          axisTick: {
            show: false,
          },
        },
        series: [
          {
            name: 'KPI',
            type: 'custom',
            renderItem: (params, api) => {
              const location = api.coord([api.value(0), api.value(1)]);
              return {
                type: 'group',
                x: 0,
                children: [
                  {
                    type: 'CubeLeft',
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: api.coord([api.value(0), 0]),
                    },
                    style: {
                      fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: '#3498ba' },
                        { offset: 1, color: '#3c7cb7' },
                      ]),
                      stroke: 'rgba(3, 25, 63, .1)',
                    },
                  },
                  {
                    type: 'CubeRight',
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: api.coord([api.value(0), 0]),
                    },
                    style: {
                      fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: 'rgba(0, 81, 218, 1)' },
                        { offset: 1, color: 'rgba(0, 81, 218, 1)' },
                      ]),
                      stroke: 'rgba(3, 25, 63, .1)',
                    },
                  },
                  {
                    type: 'CubeTop',
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: api.coord([api.value(0), 0]),
                    },
                    style: {
                      fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        { offset: 0, color: '#3ae3fd' },
                        { offset: 1, color: '#3ae3fd' },
                      ]),
                      stroke: 'rgba(3, 25, 63, .1)',
                    },
                  },
                ],
              };
            },
            data: data.data,
          },
          {
            type: 'bar',
            label: {
              normal: {
                show: false,
                position: 'top',
                formatter: (e) => e.value,
                fontSize: 22,
                color: '#fff',
                offset: [0, -25],
              },
            },
            itemStyle: {
              color: 'transparent',
            },
            data: data.data,
          },
        ],
      };

      this.kpiChart.setOption(option);
    },
  },
};
</script>

<style scoped>
.left-top {
  margin-left: 172px;
  margin-top: 15px;
  width: 132px;
  height: 28px;
  align-items: center;
  line-height: 28px;
  text-align: center;
  background-image: url('/public/imgs/lefis.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  font-weight: 400;
  font-size: 14px;
  color: #FFFFFF;
}

.kpi-chart {
  width: 300px !important;
  height: 190px;
  margin-bottom: 5px;
}
</style>
